package com.zunipe.minix;

import com.zunipe.ZunipeTools;
import com.zunipe.until.Util;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(ChatHud.class)
public class ChatHudMixin
{
    private boolean run = false;

    @Inject(method = "addMessage(Lnet/minecraft/text/Text;)V", at = @At("RETURN"))
    private void addMessageX(Text message, CallbackInfo info)
    {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.player == null)
            return;
        if (Util.autoRedPacket)
        {
            if ((message.getString().contains("拼手气红包") || message.getString().contains("普通红包")) && message.getString().contains("点击领取"))
            {
                List<Text> texts = message.getSiblings();
                for (Text text : texts)
                {
                    if (text.getString().contains("点击领取"))
                    {
                        Style s = text.getStyle();
                        if (s != null && s.getClickEvent() != null)
                        {
                            client.player.sendChatMessage(s.getClickEvent().getValue());
                        }
                    }
                }
            }
        }
        if (message.getString().matches(".*欢迎新玩家 \\[.+].+ 加入服务器!.*"))
        {
            new Thread(() ->
            {
                try
                {
                    int i = Util.randomInt(7500, 15000);
                    Thread.sleep(i);
                    client.player.sendChatMessage("欢迎");
                } catch (InterruptedException e)
                {
                    ZunipeTools.LOGGER.error(e.getMessage());
                }
            }).start();
        }
        else if (message.getString().matches(".*\\[公告] 恭喜玩家.+升级到 \\[.+].*"))
        {
            if (!run)
            {
                new Thread(() ->
                {
                    run = true;
                    try
                    {
                        int i = Util.randomInt(7500, 15000);
                        Thread.sleep(i);
                        client.player.sendChatMessage("恭喜");
                    } catch (InterruptedException e)
                    {
                        ZunipeTools.LOGGER.error(e.getMessage());
                    }
                    run = false;
                }).start();
            }
        }
    }
}
